{% extends 'home/template.html' %}

{% load i18n %}
{% load martortags %}
{% load bleach_tags %}

{% block title %} {% translate "Product Details" %} {% endblock title %}

{% block stylesheets %}
  {{ block.super }}
{% endblock stylesheets %}

{% block content %}
  

    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">

          <div class="col-sm-6">
            <h1>{% translate "Product" %} {{ DB_product_query.name | bleach }}</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/">{% translate "Home" %}</a></li>
              <li class="breadcrumb-item"><a href="{% url 'customer_view' DB_product_query.customer.pk %}">{{ DB_product_query.customer.name | bleach}}</a></li>
              <li class="breadcrumb-item active">{{ DB_product_query.name | bleach }}</li>
            </ol>
          </div>
        </div>
      </div>
    </section>



    <section class="content">
      <div class="container-fluid">
      <hr>

      <div class="row">
        <div class="col-lg-3 col-xs-6">

          <div class="small-box bg-info">
            <div class="inner">
              <h3>{{count_product_report}}</h3> 
              <p>{% translate "Report" %}{{ count_product_report|pluralize:"" }}</p>
            </div>
            <div class="icon">
              <i class="fas fa-file-code"></i>
            </div>
          </div>
        </div>

        <div class="col-lg-3 col-xs-6">

          <div class="small-box bg-green">
            <div class="inner">
              <h3>{{product_findings}}</h3>

              <p>{% translate "Finding" %}{{ product_findings|pluralize:"" }}</p>
            </div>
            <div class="icon">
              <i class="fa fa-bug"></i>
            </div>
          </div>
        </div>

        <div class="col-lg-3 col-xs-6">

          <div class="small-box bg-yellow">
            <div class="inner">
              <h3>{{count_product_findings_medium}}</h3>

              <p>{% translate "Medium Severity Finding" %}{{ count_product_findings_medium|pluralize:"" }}</p>
            </div>
            <div class="icon">
              <i class="fa fa-exclamation-triangle"></i>
            </div>
          </div>
        </div>

        <div class="col-lg-3 col-xs-6">

          <div class="small-box bg-red">
            <div class="inner">
              <h3>{{count_product_findings_critical_high}}</h3>

              <p>{% translate "Critical/High Severity Finding" %}{{ count_product_findings_critical_high|pluralize:"" }}</p>
            </div>
            <div class="icon">
              <i class="fas fa-radiation-alt"></i>
            </div>
          </div>
        </div>
      </div>
  


      {% if DB_product_query.description %}
      <div class="row">
        <div class="col-md-12">
            <div class="card card-outline card-primary">
                <div class="card-header">
                  <h3 class="card-title">
                    <dt>{% translate "Description" %}</dt>
                  </h3>
                  <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                      <i class="fas fa-minus"></i>
                    </button>
                  </div>
                </div>
                <div class="card-body">
                    <p>{{ DB_product_query.description | safe_markdown | bleach }}</p>
                </div>
              </div>
        </div>
      </div>
      {% endif%}



      <div class="card card-outline card-primary">
        <div class="card-header">
          <h3 class="card-title">
            <dt>{% translate "Reports" %}</dt>
          </h3>
          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
          </div>
        </div>
        <div class="card-body">
          <table id="reportlist" class="table table-bordered table-hover">
              <thead>
                  <tr>
                    <th style="width: 20%">{% translate "ID" %}</th>
                    <th style="width: 35%">{% translate "Report" %}</th>
                    <th style="width: 5%">{% translate "Date" %}</th>
                    <th style="width: 15%">{% translate "Findings" %}</th>
                    <th style="width: 25%" class="text-center">{% translate "Actions" %}</th>
                  </tr>
              </thead>
              <tbody>

              {% for report in DB_report_query %}
                  <tr>
                    <td>{{ report.report_id | bleach }} </td>
                    <td>{{ report.title | bleach }} </td>
                    <td>{{ report.creation_date | date:"d-m-Y" }} </td>
                    <td>
                      <span class="pull-right badge bg-red">{{ report.db_finding_set.count }}</span> {% translate "Finding" %}{{ report.db_finding_set.count|pluralize:"" }}
                    </td>
                    <td class="text-center">
                        <a href="/report/view/{{ report.pk }}"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-folder"></i> {% translate "View" %}</button></a>
                        
                        {% if user.groups.all.0|stringformat:'s' == "administrator" %}
                          <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target=".mm-modal-clone-{{report.pk}}"><i class="fas fa-copy"></i> {% translate "Clone" %} </button>
                          <a href="/report/edit/{{ report.pk }}"><button type="button" class="btn btn-info btn-sm"><i class="fa fa-edit"></i> {% translate "Edit" %}</button></a> 
                          <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target=".mm-modal-{{report.pk}}"><i class="fas fa-trash"></i> {% translate "Delete" %} </button>
                        {% endif %}
                    </td>
                  </tr>
                  

                        <div class="modal modal-danger fade mm-modal-{{report.pk}}" id="modal-danger">
                          <div class="modal-dialog">
                            <div class="modal-content bg-danger">
                              <div class="modal-header">
                                <h4 class="modal-title">{% translate "Are you sure?" %}</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
                                <p>{% translate "Delete Report" %}&hellip;</p>
                              </div>
                              <div class="modal-footer justify-content-between">
                                <button type="button" class="btn btn-outline-light" data-dismiss="modal">{% translate "Close" %}</button>

                                <button type="button" onclick="DeleteID({{report.pk}});" class="btn btn-outline-light">{% translate "Delete Report" %}</button>
                              </div>
                            </div>
                          </div>
                        </div>


                        <div class="modal modal-warning fade mm-modal-clone-{{report.pk}}" id="modal-warning">
                          <div class="modal-dialog">
                            <div class="modal-content bg-warning">
                              <div class="modal-header">
                                <h4 class="modal-title">{% translate "Are you sure?" %}</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
                                <p>{% translate "Clone Report and Findings" %}&hellip;</p>
                              </div>
                              <div class="modal-footer justify-content-between">
                                <button type="button" class="btn btn-outline-light" data-dismiss="modal">{% translate "Close" %}</button>
                                <button type="button" onclick="DuplicateID({{report.pk}});" class="btn btn-outline-light">{% translate "Clone Report and Findings" %}</button>
                              </div>
                            </div>
                          </div>
                        </div>

                {% endfor %}

              </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>

<br>


{% endblock content %}

{% block javascripts %}
  {{ block.super }}

<script>
  $(function () {
    $("#reportlist").DataTable({
      "paging": true,
      "searching": true,
      "responsive": true, 
      "lengthChange": false, 
      "autoWidth": false,
      "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#reportlist_wrapper .col-md-6:eq(0)');
  });
</script>

<script>
  function DeleteID(del_id) {
        $.ajax({
            type: 'POST',
            url: "/en/report/delete/",
            headers:{
              "X-CSRFToken": '{{ csrf_token }}'
            },
            data : { 'delete_id': del_id},

            success : function(json) {
              document.location.href = "{% url 'product_view' DB_product_query.pk %}";
            }
        })
  }


  function DuplicateID(dup_id) {
    $.ajax({
        type: 'POST',
        url: "{% url 'report_findings_duplicate' %}",
        headers:{
          "X-CSRFToken": '{{ csrf_token }}'
        },
        data : { 'duplicate_id': dup_id},

        success : function(json) {
          document.location.href = "{% url 'product_view' DB_product_query.pk %}";
        }
    })
  }
</script>

{% endblock javascripts %}
